﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace docmgr.WinForm
{
    public partial class DocumentOperation : Form
    {
        public int ID = 0;

        int handle ;

        docmgr.BLL.Department depbll = new docmgr.BLL.Department();
        docmgr.BLL.User userbll = new BLL.User();

        docmgr.BLL.Document docbll = new BLL.Document();

        DataTable docDatable = null;
        public DocumentOperation()
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

        }

        public DocumentOperation(String key)
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

            this.ID = Convert.ToInt32(key);
            doinit();
            docDatable = docbll.GetModel(key);

            /**
            String DocName=DocNameTextBox.Text;
            String Remark = RemarkTextBox.Text;
            String RegDate = DateTime.Now.ToString();
            int UserID = Convert.ToInt32(Common.curUser.Rows[0]["ID"]);
            int SysSerialNumber = Convert.ToInt32(SysSerialNumberNumericUpDown.Value);
            String DocSerialNumber = DocSerialNumberTextBox.Text;
            String SecretLevel = (String)SecretLevelcomboBox.SelectedItem;
            int DepartmentID = Convert.ToInt32(DepartmentcomboBox.SelectedValue);
            String DocType =(String) DocTypecombobox.SelectedItem;
            int PageNumber =  Convert.ToInt32(PageNumberNumericUpDown.Value);
            String Status = "在库";
            String Code = new Random().Next().ToString();
            int HandleUserID = Convert.ToInt32(Common.curUser.Rows[0]["ID"]);
           */
            DocNameTextBox.Text = (String)docDatable.Rows[0]["DocName"];
            DocTypecombobox.SelectedValue = docDatable.Rows[0]["DocType"];
            //SysSerialNumberNumericUpDown.Value = Convert.ToDecimal(docDatable.Rows[0]["SysSerialNumber"]);
            DocSerialNumberTextBox.Text = (String)docDatable.Rows[0]["DocSerialNumber"];
            SecretLevelcomboBox.SelectedItem = docDatable.Rows[0]["SecretLevel"];
            DepartmentcomboBox.SelectedValue = docDatable.Rows[0]["DepartmentID"];
            RemarkTextBox.Text=(String)docDatable.Rows[0]["Remark"];
            PageNumberNumericUpDown.Value = Convert.ToDecimal(docDatable.Rows[0]["PageNumber"]);
            HandleUsercomboBox.SelectedValue = docDatable.Rows[0]["HandleUserID"];

            handle=Convert.ToInt32(docDatable.Rows[0]["HandleUserID"]);

            if (Convert.ToInt32(Common.curUser.Rows[0]["ID"]) != Convert.ToInt32(docDatable.Rows[0]["UserID"]))
            {/**
              *非当前创建人，禁止编辑内容。
              * 实施了灭绝人性的大屠杀。
              */
                DocNameTextBox.ReadOnly = true;
                DocTypecombobox.Enabled = false;
                SysSerialNumberNumericUpDown.ReadOnly = true;
                DocSerialNumberTextBox.ReadOnly = true;
                SecretLevelcomboBox.Enabled = false;
                DepartmentcomboBox.Enabled = false;
                RemarkTextBox.ReadOnly = true;
                PageNumberNumericUpDown.ReadOnly = true;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(SecretLevelcomboBox.SelectedItem)=="绝密")
            {
                MessageBox.Show("绝密不能进行修改，如有问题请到保密室进行修改");
                return;
            }

            if (DocNameTextBox.Text == "" || RemarkTextBox.Text=="")
            {
                MessageBox.Show("不能为空");
                DocNameTextBox.Focus();
                return;
            }

            docDatable.Rows[0]["DocName"] = DocNameTextBox.Text;
            //docDatable.Rows[0]["DocType"] = DocTypecombobox.SelectedItem;//不能更改
            //docDatable.Rows[0]["SysSerialNumber"]=SysSerialNumberNumericUpDown.Value;
            //docDatable.Rows[0]["DocSerialNumber"]=DocSerialNumberTextBox.Text;
            //docDatable.Rows[0]["SecretLevel"]=SecretLevelcomboBox.SelectedItem;
            //docDatable.Rows[0]["DepartmentID"]=DepartmentcomboBox.SelectedValue;
            docDatable.Rows[0]["Remark"]=RemarkTextBox.Text;
            docDatable.Rows[0]["PageNumber"]=PageNumberNumericUpDown.Value;
            //docDatable.Rows[0]["HandleUserID"]=HandleUsercomboBox.SelectedValue;
            /**
             只有 文件标题 备注 和 页数可以修改，其他都不能修改
             */

            if (handle !=Convert.ToInt32( docDatable.Rows[0]["HandleUserID"]))
            {
                docDatable.Rows[0]["Status"] = "传阅";
                int DocumentID=Convert.ToInt32(docDatable.Rows[0]["ID"]);
                int LeaderId=handle;
                int ReadUserID=Convert.ToInt32( docDatable.Rows[0]["HandleUserID"]);
                String Status="未读";
                String Fields="DocumentID,LeaderId,Status,ReadUserID";
                String sqlString = String.Format("insert DocReadLog ({0}) values({1},{2},'{3}',{4})", Fields, DocumentID, LeaderId, Status, ReadUserID);

                SQLHelper.ExecuteSql(SQLHelper.connString, sqlString);
            }
            docbll.Update(docDatable);
            this.Close();
        }

        private void DocumentOperation_Load(object sender, EventArgs e)
        {
            
        }

        public void doinit()
        {
            Common.initCombox(DocTypecombobox, Common.DocType);
            Common.initCombox(SecretLevelcomboBox, Common.SercretLevel);

            DataTable dt = depbll.GetList("id,name", "1=1");

            this.DepartmentcomboBox.DataSource = dt;
            this.DepartmentcomboBox.DisplayMember = "Name";
            this.DepartmentcomboBox.ValueMember = "ID";

            DataTable dt1 = userbll.GetList("id,name", "1=1");

            this.HandleUsercomboBox.DataSource = dt1;
            this.HandleUsercomboBox.DisplayMember = "Name";
            this.HandleUsercomboBox.ValueMember = "ID";

            if (Convert.ToInt32(Common.curUser.Rows[0]["PositionID"]) != 1)
            {
                //持有人ID
                button2.Visible = false;//如果是管理员，一切ok。全部可以销毁。
                button1.Visible = false;
            }
        }

        private void HandleUsercomboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            using (docmgr.WinForm.InputMessagebox frm = new InputMessagebox())
            {
                frm.WindowState = FormWindowState.Normal;
                frm.StartPosition = FormStartPosition.CenterScreen;
                frm.ShowDialog();


                DataTable dt = userbll.GetModel(String.Format("{0}", HandleUsercomboBox.SelectedValue));

                if (Common.EncryptCode(frm.password) !=Convert.ToString( dt.Rows[0]["Password"]))
                {
                    MessageBox.Show("用户密码输入错误，不能转交该文件");
                    HandleUsercomboBox.SelectedValue = docDatable.Rows[0]["HandleUserID"];//重置
                }
            }
                
        }

        private void button2_Click(object sender, EventArgs e)
        {

            /**
             这个打印自己手上的所有文档。
             */
            String sqlStr = "select u.Name ,dh.Code from DocHandler as dh LEFT JOIN [User] as u on u.id=dh.HandlerUserID LEFT JOIN Document as doc on doc.ID=dh.DocID  where  dh.DocID = " + Convert.ToInt32(docDatable.Rows[0]["ID"]);



            DataTable dt = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];

            ArrayList imgs = new ArrayList();

            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                imgs.Add(new imageDocument(Convert.ToString(dt.Rows[i]["Name"]), Convert.ToString(dt.Rows[i]["Code"])));
            }
                ///打印标签
            try
            {
                foreach(imageDocument img in imgs)  
                    img.Print();
            }
            catch (Exception ex11)
            {
                MessageBox.Show("打印异常");
                Common.Endding();
            }
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            AddReadPeople frm = new AddReadPeople(docDatable);
            frm.ShowDialog();

            this.Close();
        }
    }
}
